﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TBlog.Models.ViewData;

//using System.Security.Principal;
//using System.Web.Security;

using TBlog.Models;


namespace TBlog.Controllers
{
    public class AdminController : Controller
    {
        ITBlogService _service;

        public AdminController()
        {
            if (!System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
                RedirectToAction("LogOn", "Account");
            else
                _service = new TBlogService();
        }

        [Authorize]
        public ActionResult Index()
        {
            return View("Index");
        }

        [Authorize]
        public ActionResult Category()
        {
            return View(_service.ListCategory());
        }

        /*
         *  Get Edit Category
         */

        public ActionResult EditCategory(int? id)
        {
            if (!id.HasValue)
                RedirectToAction("CreateCategory");

            return View(_service.GetCategory(id));
        }

        /*
         *  Post Edit Category
         */
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditCategory(Category categoryToEdit)
        {
            _service.EditCategory(categoryToEdit);
            return RedirectToAction("Category");

            //return View("EditCategory", categoryToEdit);
        }

        #region CreateCategory
        [Authorize]
        public ActionResult CreateCategory()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateCategory([Bind(Exclude = "cattegory_id")] Category categoryToCreate)
        {
            if (_service.CreateCategory(categoryToCreate))
                return RedirectToAction("Category");

            return View();
        }
        #endregion CreateCategory

        #region DeleteCategory
        public ActionResult DeleteCategory(int id)
        {
            return View("DeleteCategory", _service.GetCategory(id));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ActionName("DeleteCategory")]
        public ActionResult DeleteCategory(int id, int? nni)
        {
            var categoryToDelete = _service.GetCategory(id);
            _service.DeleteCategory(categoryToDelete);

            return RedirectToAction("Category");
        }
        # endregion DeleteCategory

        #region Post
        [Authorize]
        public ActionResult Post()
        {
            return View(_service.ListPost("page").ToList());
        }
        #endregion Post

        #region CreatePost
        [Authorize]
        public ActionResult CreatePost()
        {
            return View();
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreatePost([Bind(Exclude = "post_id")] Post postToCreate)
        {
            postToCreate.post_type = "post";

            if (_service.CreatePost(postToCreate))
                return RedirectToAction("Post");

            return View();
        }
        #endregion CreatePost

        #region Page
        [Authorize]
        public ActionResult Page()
        {
            return View(_service.ListPost("page"));
        }
        #endregion Page

        #region CreatePage
        [Authorize]
        public ActionResult CreatePage()
        {
            var _postPageModel = new PostPageModel
            {
                post = new Post(),
                category = _service.ListCategory().ToList()
            };
            return View(_postPageModel);
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreatePage([Bind(Exclude = "post_id")] Post pageToCreate)
        {
            pageToCreate.post_type = "page";

            if (_service.CreatePost(pageToCreate))
                return RedirectToAction("Page");

            return RedirectToAction("CreatePage");
        }
        #endregion CreatePage

        public ActionResult Setting()
        {
            return View();
        }
    }
}
